<?php
/**
 * crcl/base
 *
 * Copyright 2013 CRCL - André Sabosch - info@crcl.de
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * dealing with file URIs
 *
 * http://en.wikipedia.org/wiki/File_URI_scheme
 * RFC 1738 file://[host]/path
 * RFC 3986	file:[//host]/path
 * generic syntax: URI = scheme ":" hier-part [ "?" query ] [ "#" fragment ]
 *
 * sample:
 * without host
 *  ini:///etc/test.ini
 *  getScheme = ini (inherited from uri)
 *  getFile = test.ini
 *  getExtension = ini
 *  getPath = /etc/test.ini (inherited from uri)
 *  getDirectory = /etc
 *  getHost = localhost (inherited from uri)
 *
 * with host
 *  ini://127.0.0.1/../config.ini
 *  getHost = 127.0.01
 *
 * with relative path (localhost only)
 * ini:/../config.ini
 * getDirectory = ..
 * getPath = ../config.ini
 */

namespace crcl\base\resource\reader;

interface IFile
{
  /**
   * Gets file (incl. extension) from uri
   *
   * @return string
   */
  public function getFile();

  /**
   * Returns fie extension from uri
   *
   * @return string
   */
  public function getExtension();

  /**
   * Gets directory (without filename) from URI
   *
   * @return  string  Directory without filename
   */
  public function getDirectory();
}

class ContainerFile extends ContainerUri implements IFile
{
  public function __construct($sUri)
  {
    parent::__construct($sUri);
    try
    {
      $sPath = $this->getPath();
      $this->sFile = $this->resolveFile($sPath);
      $this->sExtension = $this->resolveFileExtension($sPath);
    }
    catch (Exception $e)
    {
      //no path, no file, no extension
      if ($e->getCode() !== Exception::ERROR_URI_NOPATH)
      {
        //right now, there is no other exception ;)
        //@codeCoverageIgnoreStart
        throw $e;
        //@codeCoverageIgnoreEnd
      }
    }
  }

  public function getFile()
  {
    return $this->sFile;
  }

  public function getExtension()
  {
    return $this->sExtension;
  }

  public function getDirectory()
  {
    $sDirectory = $this->getPath();
    $aPath = \pathinfo($sDirectory);
    if (isset($aPath['dirname']))
    {
      $sDirectory = $aPath['dirname'];
      $sDirectory = \trim($sDirectory);
      $sDirectory = \crcl\base\String::getInstance($sDirectory)->doEndWith('/')->toString();
    }
    return $sDirectory;
  }

  /** PRIVATE *************************************************************************************/

  private $sFile = '';
  private $sExtension = '';

  private function resolveFile($sPath)
  {
    $sFile = \NULL;
    $aPath = \pathinfo($sPath);
    if (isset($aPath['basename']))
    {
      $sFile = $aPath['basename'];
      $sFile = \trim($sFile);
    }
    return $sFile;
  }

  private function resolveFileExtension($sPath)
  {
    $sFileExtension = \NULL;
    if (!\is_null($this->sFile))
    {
      $aPath = \pathinfo($sPath);
      if (isset($aPath['extension']))
      {
        $sFileExtension = $aPath['extension'];
        $sFileExtension = \trim($sFileExtension);
      }
    }
    return $sFileExtension;
  }
}
/*
  public function getFile()
  {
    if (\is_null($this->_sFile))
    {
      throw new Exception(Exception::ERROR_NOFILE, array('uri' => $this->_sUri));
    }
    return $this->_sFile;
  }

  public function getFileName()
  {
    if (\is_null($this->_sFileName))
    {
      throw new Exception(Exception::ERROR_NOFILENAME, array('uri' => $this->_sUri));
    }
    return $this->_sFileName;
  }

  public function getFileExtension()
  {
    if (\is_null($this->_sFileExtension))
    {
      throw new Exception(Exception::ERROR_NOFILEEXTENSION, array('uri' => $this->_sUri));
    }
    return $this->_sFileExtension;
  }
*/